package com.kuaiyin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kuaiyin.dto.RequestDTO;
import com.kuaiyin.entity.Request;
import com.kuaiyin.exception.BaseException;
import com.kuaiyin.mapper.RequestMapper;
import com.kuaiyin.mapper.TestCaseMapper;
import com.kuaiyin.service.IRequestService;
import com.kuaiyin.utils.PageHelper;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.RequestVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 接口请求定义表（支持动态模板） 服务实现类
 * </p>
 *
 * @author ng
 * @since 2025-04-25
 */
@Slf4j
@Service
public class RequestServiceImpl extends ServiceImpl<RequestMapper, Request> implements IRequestService {
    @Autowired
    private RequestMapper requestMapper;
    @Autowired
    private TestCaseMapper testCaseMapper;

    @Override
    public void savaRequest(RequestDTO requestDTO) {
        if (lambdaQuery().eq(Request::getName, requestDTO.getName()).exists()) {
            throw BaseException.of(0, "名称已存在");
        }
        //去除路径两头的空格
        requestDTO.setPath(requestDTO.getPath().trim());
        Request request = new Request();
        BeanUtils.copyProperties(requestDTO, request);
        requestMapper.insert(request);
    }

    @Override
    public RequestVO getById(Long id) {
        Request request = super.getById(id);
        RequestVO requestVO = new RequestVO();
        BeanUtils.copyProperties(request, requestVO);
        return requestVO;
    }

    @Override
    public void updateRequest(Long id, RequestDTO requestDTO) {
        requestDTO.setId(id);
        if (lambdaQuery().eq(Request::getName, requestDTO.getName()).ne(Request::getId, id).exists()) {
            throw BaseException.of(0, "名称已存在");
        }
        //去除路径两头的空格
        requestDTO.setPath(requestDTO.getPath().trim());
        Request request = new Request();
        BeanUtils.copyProperties(requestDTO, request);
        requestMapper.updateById(request);
    }

    /**
     *  查询所有接口请求定义
     * @return  返回接口请求定义集合
     */
    @Override
    public List<RequestVO> getRequestList() {
        return PageHelper.queryAllWithVo(requestMapper, RequestVO.class);
    }

    @Override
    public void removeById(Long id) {
        //需要校验该接口是否被引用，如果被引用则不能删除
        List<Long> caseIds = testCaseMapper.selectCaseIdsByRequestId(id);
        if (caseIds != null && !caseIds.isEmpty()) {
            // 提示被哪些用例引用
            String caseIdList = caseIds.stream()
                    .map(String::valueOf)
                    .collect(Collectors.joining(", "));
            throw BaseException.of(0, "请求ID-" + id + "被以下测试用例引用，不能删除：" + caseIdList);
        }
        requestMapper.deleteById(id);
    }

    @Override
    public PageResult<RequestVO> getRequestPage(Integer page, Integer size, Consumer<LambdaQueryWrapper<Request>> condition) {
        return PageHelper.queryWithVo(page, size, requestMapper, RequestVO.class, condition);
    }


}
